﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellSerializable_h
#define CellSerializable_h

#include "CellPrerequisites.h"

namespace cell
{
/** \addtogroup Core
 *  @{
 */
/** \addtogroup General
 *  @{
 */

/** 序列化流。
@author
	Jiangwei Xu
*/
class _CellExport SerializerStream : public GeneralAlloc
{
public:
	/**
	*/
	SerializerStream(void);

	/**
	*/
	virtual ~SerializerStream(void);

public:
	/**
	*/
	void read(char* output, size_t size);

	/**
	*/
	void write(const char* input, size_t size);

	/**
	*/
	SerializerStream& seekg(size_t pos);

	/**
	*/
	size_t length(void) const;

	/**
	*/
	void str(String& str) const;

	/**
	*/
	void str(char* str) const;

	/**
	*/
	void clear(void);

	/**
	*/
	void free(void);

	/**
	*/
	void cout(void);

protected:
	typedef std::stringstream StreamCategory;

	StreamCategory mStream;

	friend SerializerStream& operator<<(SerializerStream& strm, const String& str);
	friend SerializerStream& operator<<(SerializerStream& strm, const char* str);
	friend SerializerStream& operator<<(SerializerStream& strm, char str);
	friend SerializerStream& operator<<(SerializerStream& strm, int v);
	friend SerializerStream& operator<<(SerializerStream& strm, uint v);
	friend SerializerStream& operator<<(SerializerStream& strm, long v);
	friend SerializerStream& operator<<(SerializerStream& strm, ulong v);
};

//---------------------------------------------------------------------------
inline SerializerStream& operator<<(SerializerStream& strm, const String& str)
{
	strm.mStream << str;
	return strm;
}
//---------------------------------------------------------------------------
inline SerializerStream& operator<<(SerializerStream& strm, const char* str)
{
	strm.mStream << str;
	return strm;
}
//---------------------------------------------------------------------------
inline SerializerStream& operator<<(SerializerStream& strm, char str)
{
	strm.mStream << str;
	return strm;
}
//---------------------------------------------------------------------------
inline SerializerStream& operator<<(SerializerStream& strm, int v)
{
	strm.mStream << v;
	return strm;
}
//---------------------------------------------------------------------------
inline SerializerStream& operator<<(SerializerStream& strm, uint v)
{
	strm.mStream << v;
	return strm;
}
//---------------------------------------------------------------------------
inline SerializerStream& operator<<(SerializerStream& strm, long v)
{
	strm.mStream << v;
	return strm;
}
//---------------------------------------------------------------------------
inline SerializerStream& operator<<(SerializerStream& strm, ulong v)
{
	strm.mStream << v;
	return strm;
}


/** 序列化接口。
@author
	Jiangwei Xu
*/
class _CellExport Serializable
{
public:
	/**
	*/
	Serializable(void);

	/**
	*/
	virtual ~Serializable(void);

public:
	/**
	*/
	virtual void write(SerializerStream& out) = 0;

	/**
	*/
	virtual void read(SerializerStream& in) = 0;
};

/** @} */
/** @} */
} // end namespace cell

#endif // CellSerializable_h
